package org.example.demo1;

/*
https://blog.csdn.net/feinifi/article/details/119797099
 */
//@Component
//@ServerEndpoint("/webSocket/{sid}")
//public class DemoEndPoint {
//    private static AtomicInteger onlineNum = new AtomicInteger();
//    private static ConcurrentHashMap<String, Session> sessionPools = new ConcurrentHashMap<>();
//
//    public void sendMessage(Session session, String message) throws IOException {
//        if (session != null) {
//            synchronized (session) {
//                System.out.println("发送消息内容：" + message);
//                session.getBasicRemote().sendText(message);
//            }
//        }
//    }
//
//    @OnOpen
//    public void onOpen(Session session, @PathParam(value = "sid") String sid) {
//        sessionPools.put(sid, session);
//        onlineNum.incrementAndGet();
//        System.out.println("用户 user " + sid + " add websocket,current users is " + onlineNum);
//        try {
//            sendMessage(session, "welcome to websocket");
//        } catch (Exception e) {
//            System.out.println("onOpen 发送消息异常 " + e.toString());
//        }
//    }
//
//    @OnClose
//    public void onClose(@PathParam(value = "sid") String sid) {
//        sessionPools.remove(sid);
//        onlineNum.decrementAndGet();
//        System.out.println(sid + " close websocket,current users is " + onlineNum);
//    }
//
//    @OnMessage
//    public void onMessage(String message) {
//        message = "client message：" + message + ", received";
//        System.out.println(message);
//        for (Session session : sessionPools.values()) {
//            try {
//                sendMessage(session, message);
//            } catch (Exception e) {
//                System.out.println("onMessage 发送消息异常 " + e.toString());
//            }
//        }
//    }
//
//    @OnError
//    public void onError(Session session, Throwable throwable) {
//        System.out.println("onError 回调 error occured");
//        throwable.printStackTrace();
//    }
//
//}


//--------------------------------------------------------------------------------------------------


import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

@ServerEndpoint("/webSocket/{uid}")
@Component
public class DemoEndPoint {
    private static AtomicInteger onlineNum = new AtomicInteger();

    private static ConcurrentHashMap<String, Session> sessionPools = new ConcurrentHashMap<>();

    public void sendMessage(Session session, String message) throws IOException {
        if (session != null) {
            synchronized (session) {
                System.out.println("send message -> " + message);
                session.getBasicRemote().sendText(message);
            }
        }
    }


    @OnOpen
    public void onOpen(Session session, @PathParam(value = "uid") String uid) {
        sessionPools.put(uid, session);

        onlineNum.incrementAndGet();//

        System.out.println("== 服务端 onOpen user " + uid + " add websocket,current users is " + onlineNum);

        try {
            sendMessage(session, "welcome to websocket.");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("== 服务端 onOpen 异常信息： " + e.toString());
        }

    }

    @OnClose
    public void onClose(@PathParam("uid") String uid) {
        sessionPools.remove(uid);

        onlineNum.decrementAndGet();

        System.out.println("服务端 onClose " + uid + " close websocket,current users is " + onlineNum);
    }

    @OnMessage
    public void onMessage(@PathParam("uid") String uid, String message) throws IOException {
        message = "uid = "+uid+ "client message " + message + ",received";
        System.out.println(message);

        //一对一
        try {
            sendMessage(sessionPools.get(uid), message);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //群发
       /* for (Session session : sessionPools.values()) {
            try {
                sendMessage(session, message);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }*/
    }

    @OnError
    public void onError(Session session, Throwable thrown) {
        System.out.println("== 连接 发生错误 error occured");
        thrown.printStackTrace();
    }

}